/*
obj:按钮对象
zid:专区编号
strOptions:附加参数，符合JSON格式的字符串 "{param1:1,param2:2}"
refresh:登录成功后是否刷新
callBack:回调函数 function(user,options) 其中user为登录成功后用户名，options为传入的附加参数，格式为JSON
*/
var SoDaoZoneLogin = {
    callBack: null,
    refresh: false,
    checkLogin: function(obj, zid, strOptions, refresh, callBack) {
        this.callBack = callBack;
        this.refresh = refresh;
        var visitUser = "";
        var btn = $(obj);
        var ajax = {
            url: "/zone/" + zid + "/GetLoginInfo", type: "POST", dataType: "json", async: false,
            success: function(data, textStatus) {
                if (data.userName != null)
                    visitUser = data.userName;
            }
        };
        $.ajax(ajax);

        if (visitUser == "") {
            if (obj) { setTimeout(function() { obj.blur(); }, 200); }
            sDialog.loadIFrame("/User/LoginWindows?LoginWinFromUrl="+encodeURIComponent(top.location.href), "搜道网 因为真实 所以精彩！赶快注册帐号登录，体验更多精彩乐趣！", 550, 230, true, null, "", true, "SoDaoZoneLogin.loginCallback", strOptions, "http://www.sodao.com/user/loginCallback");
        }
        return visitUser;
    },
    loginCallback: function(_wid, _user, _options) {
        sDialog.closef(_wid);
        if (this.refresh == true) {
            location.reload();
            return;
        }
        if (this.callBack)
            this.callBack(_user, _options);
    }
};
var SoDaoZoneJoin = {
    btn: null,
    callJoin: function(obj) {
        btn = $(obj);
        var zid = btn.attr("zid");
        var md = btn.attr("md");
        btn.attr("disabled", "disabled");
        var loginUser = SoDaoZoneLogin.checkLogin(obj, zid, "{zid:" + zid + ", md:'" + md + "'}", false, SoDaoZoneJoin.dojoin);
        if (loginUser != "") {
            SoDaoZoneJoin.dojoin(loginUser, { zid: zid, md: md }, function() { $("#fs" + zid).html("<span>已是粉丝</span>") });
        }
    },
    dojoin: function(user, options, callback) {
        var ajax = {
            url: "/zone/" + options.zid + "/joinzone", type: "POST", dataType: "json",
            data: { md: options.md },
            success: function(data) {
                sDialog.hideLoading();
                if (data.state == 1) {
                    if(callback)
                        callback();
                    else
                        window.location.reload();
                }
                else {
                    sDialog.hideLoading();
                    sDialog.error(data.msg, '操作失败', 300, 50);
                }
                if (SoDaoZoneJoin.btn)
                    SoDaoZoneJoin.btn.removeAttr("disabled");
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                sDialog.hideLoading();
                sDialog.error("错误信息" + errorThrown, '加入失败', 300, 50);
                if (SoDaoZoneJoin.btn)
                    SoDaoZoneJoin.btn.removeAttr("disabled");
            }
        };
        sDialog.loading("操作进行中。。。", 10000, 300);
        $.ajax(ajax);
    }
};
var SoDaoZoneMessage = {
    /************************
    * *专区粉丝发送站内信给专区
    * *功能描述：zid:接收专区，refurl:调用所在域的关闭窗口页面
    * *refurl页面内容：<script>parent.sDialog.closef('window');</script>，其中window为页面参数
    ************************/
    sendMessage: function(zid, refUrl) {
        var params = "refurl=" + refUrl + "&rnd=" + Math.random();
        sDialog.loadIFrame("/zone/" + zid + "/sendzonemsg?" + params, "发送消息", 420, 260);
    },
    /************************
    * *专区发送站内信给专区粉丝
    * *功能描述：zid:发送专区，fun:粉丝用户名，md:{zid,fun}，refurl:调用所在域的关闭窗口页面
    * *refurl页面内容：<script>parent.sDialog.closef('window');</script>，其中window为页面参数
    ************************/
    sendMsgToFan: function(zid, fun, md, refUrl) {
        if (!refUrl || refUrl == null)
            refUrl = "/Utility/CloseCrossSitePop";
        var params = "fun=" + fun + "&refurl=" + refUrl + "&rnd=" + Math.random() + "&md=" + md;
        sDialog.loadIFrame("/zone/" + zid + "/sendfanmsg?" + params, "发送消息", 420, 260);
    }
};